package com.yang.moyu.meta.quartz.controller;

import com.yang.moyu.meta.common.util.ApiResult;
import com.yang.moyu.meta.common.util.BeanMapUtil;
import com.yang.moyu.meta.quartz.entity.JobAndTrigger;
import com.yang.moyu.meta.quartz.entity.form.CronTaskParam;
import com.yang.moyu.meta.quartz.entity.form.ScheduleJobParam;
import com.yang.moyu.meta.quartz.service.ScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by 10296 on 2022/2/7
 *
 * @Description: 定时任务控制器
 */
@RestController
@RequestMapping("/scheduledJobApi")
@Api(value = "定时任务API", tags = {"定时任务"})
public class ScheduleJobController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ScheduleJobService scheduleJobService;

    @PostMapping("/listScheduleJob")
    @ApiOperation(value = "查询定时任务", notes = "查询定时任务")
    public ApiResult listScheduleJob(@RequestBody ScheduleJobParam pageParam) {
        List<JobAndTrigger> jobAndTriggers = scheduleJobService.queryScheduleJobList(new HashMap<>());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("scheduleJobVoList", jobAndTriggers);
        //获取正在运行的任务列表
        List<JobAndTrigger> executingJobList = scheduleJobService.queryExecutingJobList();
        resultMap.put("executingJobList", executingJobList);

        return ApiResult.ok(resultMap);
    }

    @PostMapping("/addScheduleJob")
    @ApiOperation(value = "添加定时任务", notes = "添加定时任务")
    public ApiResult addScheduleJob(@RequestBody CronTaskParam pageParam) {
        Map<String, Object> paramsMap = BeanMapUtil.bean2Map(pageParam);
        scheduleJobService.addJob(paramsMap);

        return ApiResult.ok("");
    }

    @PostMapping("/cronScheduleJob")
    @ApiOperation(value = "重新配置定时任务", notes = "重新配置定时任务")
    public ApiResult updateScheduleJob(@RequestBody CronTaskParam pageParam) {

        try {
            Map<String, Object> paramsMap = BeanMapUtil.bean2Map(pageParam);
            scheduleJobService.cronJob(paramsMap);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ApiResult.ok("");
    }

    @PostMapping("/delScheduleJob")
    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    public ApiResult delScheduleJob(@RequestBody CronTaskParam pageParam) {
        Map<String, Object> paramsMap = BeanMapUtil.bean2Map(pageParam);
        scheduleJobService.deleteJob(paramsMap);
        return ApiResult.ok("");
    }

    @PostMapping("/pauseScheduleJob")
    @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")
    public ApiResult pauseScheduleJob(@RequestBody CronTaskParam pageParam) {

        Map<String, Object> paramsMap = BeanMapUtil.bean2Map(pageParam);

        scheduleJobService.pauseJob(paramsMap);
        return ApiResult.ok("");
    }

    @PostMapping("/resumeScheduleJob")
    @ApiOperation(value = "恢复定时任务", notes = "恢复定时任务")
    public ApiResult resumeScheduleJob(@RequestBody CronTaskParam pageParam) {
        Map<String, Object> paramsMap = BeanMapUtil.bean2Map(pageParam);
        scheduleJobService.resumeJob(paramsMap);

        return ApiResult.ok("");
    }


}
